<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>施工团队</title>
    <style>
    </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
       <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">用户管理</a><span lay-separator="">/</span>
        <a><cite>代理商列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>

<div class="x-body" style="margin: 20px auto auto 15px;">
    <div class="layui-row">
        <div class="layui-input-inline">
            <input type="text" id="search_term" placeholder="请输入内容" class="layui-input">
        </div>
        <div class="layui-input-inline">
            <button class="layui-btn layui-btn-checked" lay-submit="" lay-filter="sreach" id="searchBtn"><i
                    class="layui-icon layui-icon-search"></i>查询
            </button>
        </div>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
        <script type="text/html" id="toolbarDemo">
            <button class="layui-btn layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>开设账号
            </button>
        </script>
        <script type="text/html" id="table-useradmin-webuser">
            <a class="layui-btn  layui-btn-xs" lay-event="xiaji"><i class="layui-icon layui-icon-form"></i>下级</a>
            <a class="layui-btn  layui-btn-xs" lay-event="chanel"><i class="layui-icon layui-icon-form"></i>取消代理权限</a>
            <a class="layui-btn  layui-btn-xs" lay-event="view"><i class="layui-icon layui-icon-form"></i>查看</a>
            <!--            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>-->
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
            <!--            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="setRole"><i class="layui-icon layui-icon-delete"></i>设置角色</a>-->
        </script>
        <script type="text/html" id="table-select">
            <a class="layui-btn  layui-btn-xs" lay-event="shebei"><i class="layui-icon layui-icon-form"></i>下级</a>
        </script>
    </div>
</div>


<!--弹出查看下级-->
<div id="hidden1" lay-filter="hidden1" style="display: none">
    <div style="margin-left: 20px;" class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" placeholder="请输入名称..." autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="margin-left: -15px;margin-top: -3px">
                <button class="layui-btn layui-btn-checked" lay-submit="" lay-filter="plant-search"><i
                        class="layui-icon layui-icon-search"></i>查询
                </button>
            </div>
        </div>
    </div>
    <div style="width:100%;margin-left: 10px">
        <table id="department_xiaji" lay-filter="department_xiaji"></table>
    </div>
    <script type="text/html" id="hidden1-table-tool">
        <a class="layui-btn layui-btn-xs" lay-event="select">选择</a>
    </script>
</div>


<!--弹出修改页面-->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form class="layui-form" action="" lay-filter="component-form-element-a" id="myform">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">*用户账号</label>
            <div class="layui-input-inline">
                <input type="text" name="userName" lay-verify="userName" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*用户密码</label>
            <div class="layui-input-inline">
                <input type="text" name="passWord" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*用户名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*地址</label>
            <div class="layui-input-inline">
                <input type="text" name="address" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*个人/企业</label>
            <div class="layui-input-inline">
                <input type="radio" name="idType" lay-verify="required" class="layui-input" title="个人" value="0">
                <input type="radio" name="idType" lay-verify="required" class="layui-input" title="企业" value="1">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">*身份证号/社会信用代码</label>
            <div class="layui-input-inline">
                <input type="text" name="idCard" lay-verify="idCard" placeholder="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">*联系方式</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" lay-verify="phone" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*开户行名称</label>
            <div class="layui-input-inline">
                <input type="text" name="bankName" lay-verify="required"  placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*银行卡号</label>
            <div class="layui-input-inline">
                <input type="text" name="bankNum" lay-verify="bankNum" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">固定电话</label>
            <div class="layui-input-inline">
                <input type="text" name="telePhone" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" name="email" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element-a">立即提交</button>
            </div>
        </div>
    </form>
</div>


<!--弹出查看-->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin-a"
     id="layuiadmin-form-useradmin-a"
     style="padding: 15px;display: none">
    <form class="layui-form" action="" lay-filter="component-form-element-b" id="myform1">
        <input type="hidden" name="id">
        <input type="hidden" name="saleNumber">
        <div class="layui-form-item">
            <label class="layui-form-label">用户账号</label>
            <div class="layui-input-inline">
                <input type="text" name="userName" lay-verify="userName" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户密码</label>
            <div class="layui-input-inline">
                <input type="text" name="passWord" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required"  placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-inline">
                <input type="text" name="address" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <!--????-->
        <div class="layui-form-item">
            <label class="layui-form-label">个人/企业</label>
            <div class="layui-input-inline">
                <input type="radio" name="idType" lay-verify="required" class="layui-input" title="个人" value="0">
                <input type="radio" name="idType" lay-verify="required" class="layui-input" title="企业" value="1">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">身份证号/社会信用代码</label>
            <div class="layui-input-inline">
                <input type="text" name="idCard" lay-verify="idCard" placeholder="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" lay-verify="phone" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开户行名称</label>
            <div class="layui-input-inline">
                <input type="text" name="bankName" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">银行卡号</label>
            <div class="layui-input-inline">
                <input type="text" name="bankNum" lay-verify="bankNum" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">固定电话</label>
            <div class="layui-input-inline">
                <input type="text" name="telePhone" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" name="email" lay-verify="required" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名下用户数</label>
            <div class="layui-input-inline">
                <input type="text" name="userNumber" lay-verify="required" readonly="readonly" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">注册时间</label>
            <div class="layui-input-inline">
                <input type="text" name="createDate" readonly="readonly" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">到期时间</label>
            <div class="layui-input-inline">
                <input type="text" name="endTime" readonly="readonly" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">总销售额</label>
            <div class="layui-input-inline">
                <input type="text" name="saleMoney"  readonly="readonly" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">待提现金额</label>
            <div class="layui-input-inline">
                <input type="text" name="withdrawalMoney" readonly="readonly" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element-b">立即提交</button>
            </div>
        </div>

    </form>
</div>
<script type="text/html" id="selectRepairItemName">
    <select name="RepairItemName" lay-filter="RepairItemName" data-value="">
        <option value="2">基础版10套</option>
        <option value="3">高级版3套</option>
    </select>
</script>
<%include("/common/foot.html"){}%>
<script>
    layui.use(['index', 'table', 'jquery', 'form', 'laytpl', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui', 'AliangCommon'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;
        var laytpl = layui.laytpl;

        var title = "开设账户";
        var findAllAccount_url = "system/agent/findAll.jhtml";
        var openAccount_url = "system/agent/openAccount.jhtml";
        var delete_url = "system/agent/delete.jhtml";
        var chanel_url = "system/agent/chanel.jhtml"
        var index;//定义建立富文本编辑器全局变量
        var layedit;//定义富文本编辑器layedit全局变量
        var cols = [ //表头
            // {field:'id',title:'用户ID号',align:'center'},

            {field: 'name', title: '用户名称', align: 'center'},
            {field: 'userName', title: '账号', align: 'center'},
            {field: 'phone', title: '联系方式', align: 'center'},
            {field: 'address', title: '地址', align: 'center'},
            {field: 'userNumber', title: '名下用户数', align: 'center'},
            // {field:'usernum',title:'销售设备总套数',align:'center',templet: "#selectRepairItemName"
            // },
            {field: 'saleMoney', title: '总销售额', align: 'center'},
            {field: 'withdrawalMoney', title: '待提现金额', align: 'center'},
            {
                field: 'createDate',
                title: '注册时间',
                align: 'center',
                templet: "<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd ')}}</div>"
            },
            {title: '操作', fixed: 'right', width: 400, toolbar: '#table-useradmin-webuser', align: 'center'}

        ];
        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAllAccount_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );
        //根据名称筛选内容查询
        var keyWord;
        $('#searchBtn').on('click', function () {
            keyWord = $('#search_term').val();
            if ("" == keyWord || undefined == keyWord) {
                window.location.reload();
            } else {
                table.reload('listReload', {
                    url: findAllAccount_url,
                    where: {
                        name: keyWord
                    },
                    page: {
                        curr: 1//重新从第 1 页开始
                    }
                });
            }
        });
        //弹出修改页面调用初始化数据方法
        var findRoleName_url = "role/json_role.jhtml";
        var title1 = "开设账号";
        var size = ['100%', '100%'];

        //给代理商开设账号页面
        function initUpdate(data) {
            layer.open({
                type: 1,
                title: title1,
                area: size,
                content: $('#layuiadmin-form-useradmin'),
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });

            //富文本表单验证
            form.verify({
                userName: function(value){
                    if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                        return '用户名不能有特殊字符';
                    }
                    if(/(^\_)|(\__)|(\_+$)/.test(value)){
                        return '用户名首尾不能出现下划线\'_\'';
                    }
                    if(/^\d+\d+\d$/.test(value)){
                        return '用户名不能全为数字';
                    }
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "account/validateUserName.jhtml",
                        type: "post",
                        data: {
                            "userName":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "该账号已存在";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                idCard:function (value) {
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "account/validateIdCard.jhtml",
                        type: "post",
                        data: {
                            "idCard":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "该身份证号已存在";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                phone:function(value){
                    // if(!new RegExp("/^1\\d{10}$/").test(value)){
                    //     return '请输入正确的手机号';
                    // }
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "account/validatePhone.jhtml",
                        type: "post",
                        data: {
                            "phone":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "该手机号码已存在";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                bankNum:function (value) {
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "validate/validateBankNum.jhtml",
                        type: "post",
                        data: {
                            "bankNum":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "请换一张银行卡";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                required: [/[\S]+/, "必填项不能为空"]
            });
            if (data == 0 || data == null) {
                $('#myform')[0].reset();
                return;
            } else {
                layui.form.val("component-form-element-b", loadData(data));
            }
        }

        var newTitle = "编辑信息";

        //弹出查看页面调用初始化数据方法
        function initUpdate1(data) {
            layer.open({
                type: 1,
                title: newTitle,
                area: ['100%', '100%'],
                content: $('#layuiadmin-form-useradmin-a')
            });
            layui.form.val("component-form-element-b", loadData(data));
        }

        var findAgentSaleMan_url="system/agent/findAgentSaleMan.jhtml";
        function xiaji(data) {
            layer.open({
                type: 1,
                title: "销售员信息",
                area: ['100%', '100%'],
                content: $('#hidden1')
            });

            var colss = [ //设备信息列表表头
                {field: 'id', align: 'center', fixed: 'left', title: '用户ID',width:20}
                , {field: 'name', align: 'center', title: '姓名',width:200}
                , {field: 'phone', align: 'center', title: '联系方式',width:200}
                , {field: 'address', align: 'center', title: '地址',width:200}
                , { fixed:'right',align: 'center', title: '操作', toolbar: '#hidden1-table-tool',width:200}
            ];
            AliangLayui.createTable(
                "#department_xiaji",
                "pantable",
                null,
                "销售员信息",
                findAgentSaleMan_url,
                true,
                [10, 15, 20, 25],
                ['filter', 'print', 'exports'],
                colss,
                {
                    agent_id:data                }
            );
        }

        //弹出修改
        function updateData(data) {
            var title1 = "编辑信息";
            layer.open({
                type: 1,
                title: title1,
                area: ['100%', '100%'],
                content: $('#layuiadmin-form-useradmin'),
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });

            layui.form.val("component-form-element-b", loadData(data));
        }

        function loadData(data) {
            var ret = {
                "id": data.id,
                "name": data.name,
                "passWord": data.passWord,
                "phone": data.phone,
                "userNumber": data.userNumber,
                "address": data.address,
                "createDate": data.createDate,
                "saleMoney":data.saleMoney,
                "withdrawalMoney":data.withdrawalMoney,
                "bankNum":data.bankNum,
                "bankName":data.bankName,
                "telePhone":data.telePhone,
                "email":data.email,
                "idCard":data.idCard,
                "userName":data.userName,
                "endTime":data.endTime,
                "saleNumber":data.saleNumber
            };
            if (data.idType==0){
                $("input:radio[value=0]").attr("checked","checked")
            }else{
                $("input:radio[value=1]").attr("checked","checked")
            }
            form.render();
            return ret;
        }


        //验证通过后提交数据到后台
        form.on('submit(component-form-element-a)', function (data) {
            //console.log(JSON.stringify(data.field))
            openAccount(data.field);
            return false;
        });

        form.on('submit(component-form-element-b)',function (data) {
            openAccount(data.field);
            return false;
        })

        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id;//得到复选框的值
                            postDelete(a);
                        }
                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                updateData(data);
            } else if (obj.event === 'view') {
                initUpdate1(data);
            } else if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
                    postDelete(data.id);
                });
            } else if (obj.event === 'chanel') {
                layer.confirm('确定取消代理权限吗？', function (index) {
                    postChanel(data.id);
                });
            } else if (obj.event === 'xiaji') {
                xiaji(data.id);
            }
        });

        //提交取消权限
        window.postChanel = function (id) {
            $.ajax({
                url: chanel_url,
                type: "POST",
                data: {
                    "id": id
                },
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("取消失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("取消成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                }
            })
        }
        //ajax提交删除内容\
        window.postDelete = function (id) {
            $.ajax({
                url: delete_url,
                type: "POST",
                data: {
                    "id": id
                },
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                }
            })
        };

        //开设代理商账户ajax
        window.openAccount = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data));
            $.ajax({
                url: openAccount_url,
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            table.reload('listReload');
                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        };

        window.UpDateAccount = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data));
            $.ajax({
                url: "system/openAccount/upDateAccount.jhtml",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            table.reload('listReload');
                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        };
    });

</script>
</body>
</html>
